
cc.Class({
    extends: cc.Component,

    properties: {
        confbar:cc.Node,
        players:cc.Node,
        readyBtn:cc.Node,
        mjLayout:cc.Node,
        opreation:cc.Node,
        
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        game.curGame.gameScript = this.node.getComponent('Ycmj');
        
    },

    start () {
        game.curGame.listenMsg();
        this.init();
    },
    init(){
        this.initGameConf();
        this.initPlayerInfo();
        this.initBtn();
    },
    initGameConf(){
        this.confbar.getChildByName('tid').getComponent(cc.Label).string = `房间号:${game.curGame.tid}`;
        this.confbar.getChildByName('isbp').getComponent(cc.Label).string = game.curGame.isCanBP === 0 ? '无(买+漂)':'有(买+漂)';
        this.confbar.getChildByName('maxSeatNum').getComponent(cc.Label).string = `${game.curGame.maxSeatNum}人局`;
        this.confbar.getChildByName('gameNum').getComponent(cc.Label).string = `总局数:${game.curGame.gameNum}`;
        this.confbar.getChildByName('baseScore').getComponent(cc.Label).string = `底分:${game.curGame.baseScore}`;
    },
    initPlayerInfo(){
        let seatPlayer = game.curGame.seatPlayer;
        let MjArrange = this.mjLayout.getComponent('MjArrange');
        for(let i = 0;i < 4;i++){
            let isExit = false;
            //let index = game.curGame.convertSeatId(sp.seatId);
            for(let j = 0;j< seatPlayer.length;j++){
                let sp = seatPlayer[j];
                if(sp){
                    let seatId = game.curGame.convertSeatId(sp.seatId);
                    if(seatId == i){
                        let player = this.players.getChildByName('player_'+seatId).getComponent('playerInfo');
                        if(player){
                            player.init({
                                score:sp.score,
                                sex:sp.sex,
                                nickName:sp.name,
                                state:sp.state
                            })
                        };
                        let handCard = sp.handCard;
                        if(seatId == 0){
                            MjArrange.initCardMe(handCard,sp.mocard);
                        }else if(seatId == 1){
                            MjArrange.initCardLeft(handCard,sp.mocard);
                        }else if(seatId == 2){
                            MjArrange.initCardArc(handCard,sp.mocard);
                        }else if(seatId == 3){
                            MjArrange.initCardRight(handCard,sp.mocard);
                        };
                        isExit = true;
                        break;
                    };
                }
            }
            this.players.getChildByName('player_'+i).active = isExit;
            this.mjLayout.getChildByName('card_'+i).active = isExit;
        }
        // for(let i=0;i<seatPlayer.length;i++){
        //     let sp = seatPlayer[i];
        //     if(sp){
        //         let seatId = game.curGame.convertSeatId(sp.seatId);
        //         let player = this.players.getChildByName('player_'+seatId).getComponent('playerInfo');
        //         if(player){
        //             player.init({
        //                 score:sp.score,
        //                 sex:sp.sex,
        //                 nickName:sp.name,
        //                 state:sp.state
        //             })
        //         };
        //         let handCard = sp.handCard;
        //         if(seatId == 0){
        //             let MjArrange = this.mjLayout.getComponent('MjArrange');
        //             MjArrange.initCardMe(handCard,sp.mocard);
        //         }
        //     }
        // }
    },
    initBtn(){
        this.readyBtn.active = true;
    },
    gameStart(msg){
        cc.log('gameStart---->,gameStart----->',msg);
        let countDownSprite = this.node.getChildByName('cout_down').getComponent('countDown');
        countDownSprite.updateTime(msg.opTime);
        for(let i = 0;i < 4;i++){
            let player = this.players.getChildByName('player_'+i).getComponent('playerInfo');
            if(player){
                player.updateState(3);
            };
        };
    },
    playerJoin(data){
        cc.log('playerJoin---->,YCMJ----->',data);
        let seatId = game.curGame.convertSeatId(data.seatId);
        let player = this.players.getChildByName('player_'+seatId).getComponent('playerInfo');
        if(player){
            player.init({
                score:data.score,
                sex:data.sex,
                nickName:data.name,
                state:data.state
            })
        }
    },
    playerReady(data){
        cc.log('playerJoin---->,YCMJ----->',data);
        let seatId = game.curGame.convertSeatId(data.seatId);
        let player = this.players.getChildByName('player_'+seatId).getComponent('playerInfo');
        if(player){
            player.updateState(data.state);
        }
    },
    opreateClick(event,type){
        pomelo.request('ycmj.ycmjHandler.operate', {type: type}, (res)=>{
            console.log('res======>',res);
            if(res.code != -5 ){
                this.opreation.active = false;
            } 
        });
    },
    gameOpration(data){
        cc.log('gameOpration=====>',data);
        let dircation = game.curGame.convertSeatId(data.seatId);
        if(dircation === 0){
            this.opreation.active = false;
        };
        let MjArrange = this.mjLayout.getComponent('MjArrange');
        let card = data.card;
        if(data.type == 3){
            MjArrange.peng(dircation,card);
        }else if(data.type == 0 || data.type == 1 || data.type == 2){
            let lcard = data.lcard;
            MjArrange.chi(dircation,card,lcard);
        }else if(data.type == 4){
            MjArrange.minggang(dircation,card);
        }else if(data.type == 5){
            MjArrange.angang(dircation,card);
        }else if(data.type == 6){
        }else if(data.type == 7){
        }
    },
    opreate(data){
        this.opreation.active = true;
        this.opreation.getChildByName('pass_btn').active = true;
        let MjArrange = this.mjLayout.getComponent('MjArrange');
        data.forEach((op,index)=>{
            if(op.type == 3){
                let pengNode = this.opreation.getChildByName('peng');
                pengNode.active = true;
                let data = op.cards;
                let children = pengNode.children;
                children.forEach((c,i)=>{
                    if(c.name != 'peng_btn'){
                        c.getComponent(cc.Sprite).spriteFrame = MjArrange.getCardSprit(data);
                    }
                });
            }else if(op.type === 4 || op.type === 5 || op.type === 6){
                let gangNode = this.opreation.getChildByName('gang');
                gangNode.active = true;
                let data = op.cards;
                let children = gangNode.children;
                children.forEach((c,i)=>{
                    if(c.name != 'peng_btn'){
                        c.getComponent(cc.Sprite).spriteFrame = MjArrange.getCardSprit(data);
                    }
                });
            }else if(op.type == 0){
                let chiNode = this.opreation.getChildByName('zuochi');
                chiNode.active = true;
                let data = op.cards;
                data.push(op.lcard);
                cc.log('cards====>',data);
                data.sort((a,b)=>{
                    if(MjArrange.getType(a.data) == MjArrange.getType(b.data)){
                        return MjArrange.getValue(a.data) - MjArrange.getValue(b.data);
                    }else{
                        return MjArrange.getType(a.data) - MjArrange.getType(b.data);
                    }
                });
                let children = chiNode.children;
                for(let i = 1 ; i < children.length;i++){
                    let cd = children[i];
                    cd.getComponent(cc.Sprite).spriteFrame = MjArrange.getCardSprit(data[i-1]);
                };
            }else if(op.type == 1){
                let chiNode = this.opreation.getChildByName('zhongchi');
                chiNode.active = true;
                let data = op.cards;
                data.push(op.lcard);
                cc.log('cards====>',data);
                data.sort((a,b)=>{
                    if(MjArrange.getType(a.data) == MjArrange.getType(b.data)){
                        return MjArrange.getValue(a.data) - MjArrange.getValue(b.data);
                    }else{
                        return MjArrange.getType(a.data) - MjArrange.getType(b.data);
                    }
                });
                let children = chiNode.children;
                for(let i = 1 ; i < children.length;i++){
                    let cd = children[i];
                    cd.getComponent(cc.Sprite).spriteFrame = MjArrange.getCardSprit(data[i-1]);
                };
            }else if(op.type == 2){
                let chiNode = this.opreation.getChildByName('youchi');
                chiNode.active = true;
                let data = op.cards;
                data.push(op.lcard);
                cc.log('cards====>',data);
                data.sort((a,b)=>{
                    if(MjArrange.getType(a.data) == MjArrange.getType(b.data)){
                        return MjArrange.getValue(a.data) - MjArrange.getValue(b.data);
                    }else{
                        return MjArrange.getType(a.data) - MjArrange.getType(b.data);
                    }
                });
                let children = chiNode.children;
                for(let i = 1 ; i < children.length;i++){
                    let cd = children[i];
                    cd.getComponent(cc.Sprite).spriteFrame = MjArrange.getCardSprit(data[i-1]);
                };
            }else if(op.type == 9){
                let huNode = this.opreation.getChildByName('hu_btn');
                huNode.active = true;
            }
        });
    },
    gameDealCard(data){
        for(let i=0;i<data.length;i++){
            let seatId = game.curGame.convertSeatId(data[i].seatId);
            let MjArrange = this.mjLayout.getComponent('MjArrange');
            if(seatId == 0){
                MjArrange.initCardMe(data[i].handCard,-1);
            }else if(seatId == 1){
                MjArrange.initCardLeft(data[i].handCard);
            }else if(seatId == 2){
                MjArrange.initCardArc(data[i].handCard);
            }else if(seatId == 3){
                MjArrange.initCardRight(data[i].handCard);
            }
        }
    },
    notiyOutCard(msg){
        let countDownSprite = this.node.getChildByName('cout_down').getComponent('countDown');
        let seatId = game.curGame.convertSeatId(msg.curOperator);
        let MjArrange = this.mjLayout.getComponent('MjArrange');
        if(seatId == 0){
            MjArrange.initMoCard(msg.moCard);
        }else if(seatId == 1){
            MjArrange.initMoCardLeft(msg.moCard);
        }else if(seatId == 2){
            MjArrange.initMoCardArc(msg.moCard);
        }else if(seatId == 3){
            MjArrange.initMoCardRight(msg.moCard);
        };
        countDownSprite.setDirection(seatId);
        countDownSprite.updateTime(msg.opTime);
    },
    outCard(msg){
        let seatId = game.curGame.convertSeatId(msg.seatId);
        let MjArrange = this.mjLayout.getComponent('MjArrange');
        if(seatId == 0){
            MjArrange.addOutCard(msg.card);
        }else if(seatId == 1){
            MjArrange.outCardLeft(msg.card);
        }else if(seatId == 2){
            MjArrange.outCardArc(msg.card);
        }else if(seatId == 3){
            MjArrange.outCardRight(msg.card);
        };
    },
    exitGame(){
        pomelo.request('ycmj.ycmjHandler.gameExit',{},(res)=>{
            if(res.code != -5 && res.code != 500){
                console.log('res.msg----hall.hallHandler.joinRoom->',res.msg);
                cc.director.loadScene('Hall');

            }else{

            }
        });
        //this.node.destroy();
    },
    ready(){
        pomelo.request('ycmj.ycmjHandler.ready',{},(res)=>{
            if(res.code != -5 && res.code != 500){
                console.log('ycmj.ycmjHandler.ready->',res.msg);
                this.readyBtn.active = false;
            }else{

            }
        });
    },
    update (dt) {

    },
});
